package com.yunzheng.config;


import com.yunzheng.interceptor.MyAuthFilter;
import com.yunzheng.interceptor.MyPermFilter;
import com.yunzheng.interceptor.MyRoleFilter;
import com.yunzheng.mapper.SysPermMapper;
import com.yunzheng.util.ShiroRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author: 郭思良
 * @date: 2020/2/4 21:25
 */

@Configuration
public class ShiroConfig {

	@Resource
	private SysPermMapper sysPermMapper;


	/**
	 * 过滤静态资源，拦截
	 */
	@Bean(name = "shiroFilter")
	public ShiroFilterFactoryBean shiroFilter() {
		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		shiroFilterFactoryBean.setSecurityManager(securityManager());

		//自定义拦截器
		Map<String, Filter> filterMap = new LinkedHashMap<>();
		filterMap.put("authc", new MyAuthFilter());
		filterMap.put("roles", new MyRoleFilter());
		filterMap.put("perms", new MyPermFilter());
		shiroFilterFactoryBean.setFilters(filterMap);

		//资源拦截
		Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();

		//静态资源 --- 放行
		filterChainDefinitionMap.put("/error", "anon");
		filterChainDefinitionMap.put("/swagger-resources/**", "anon");
		filterChainDefinitionMap.put("/webjars/**", "anon");
		filterChainDefinitionMap.put("/static/**", "anon");
		filterChainDefinitionMap.put("/**.html", "anon");
		filterChainDefinitionMap.put("/v2/**", "anon");
		filterChainDefinitionMap.put("/swagger-ui.html", "anon");
		//接口资源 --- 只写不放行的接口
//		List<SysPerm> sysPerms = sysPermMapper.selectList(new QueryWrapper<SysPerm>().orderByAsc(SysPerm.COL_SORT_NO));
//		for (SysPerm sysPerm : sysPerms) {
//			filterChainDefinitionMap.put(sysPerm.getUrl(), sysPerm.getDescription());
//		}

		//剩余的所有资源 --- 放行
		filterChainDefinitionMap.put("/**", "anon");
		shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
		return shiroFilterFactoryBean;

	}

	@Bean(name = "manager")
	public DefaultWebSecurityManager securityManager() {
		DefaultWebSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
		defaultSecurityManager.setRealm(shiroRealm());
		defaultSecurityManager.setSessionManager(sessionManager());
		return defaultSecurityManager;
	}

	@Bean(name = "sessionManager")
	public DefaultWebSessionManager sessionManager() {
		DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
		sessionManager.setGlobalSessionTimeout(21600000L);
		sessionManager.setDeleteInvalidSessions(true);
		sessionManager.setSessionValidationSchedulerEnabled(true);
		return sessionManager;
	}

	@Bean(name = "shiroRealm")
	public ShiroRealm shiroRealm() {
		return new ShiroRealm();
	}


}
